package com.wufun.overseas.adapters;

/**
 * Created by tangbl on 2017/12/14 0014.
 */

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
/**
 * Created by hest on 16/5/5.
 * RecyclerView 适配器基础类
 */
public abstract class RecyclerViewBaseAdapter<T, V extends View> extends RecyclerView.Adapter<RecyclerViewBaseAdapter.ViewWrapper<V>> {
    private static final String TAG = RecyclerViewBaseAdapter.class.getSimpleName();
    public SparseArray<T> arrys;
    int flag;
    Context mContext;

    public void setAdapterData(SparseArray<T> sparseArray) {
        this.arrys = sparseArray;
    }

    public SparseArray<T> getArrys(){
        return arrys;
    }

    public void setRecycleFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public int getItemCount() {
        return arrys.size();
    }

    @Override
    public ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewWrapper<V>(onCreateItemView(parent, viewType));
    }

    protected abstract V onCreateItemView(ViewGroup parent, int viewType);

    static class ViewWrapper<V extends View> extends RecyclerView.ViewHolder {

        private V view;

        public ViewWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }

    public void addItem(final int position, final T t) {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //此时已在主线程中，可以更新UI了
                arrys.put(position, t);
                notifyItemInserted(position);
            }
        });
    }

    public void removeItem(final int position) {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //此时已在主线程中，可以更新UI了
                if(position>arrys.size()){
                    return;
                }
                arrys.remove(position);
                notifyItemRemoved(position);
            }
        });
    }

    //主要用于从任意位置删除并且添加
    public void removeFromMiddleItem(final int position) {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //此时已在主线程中，可以更新UI了
                if(position>arrys.size()){
                    return;
                }else if(position==arrys.size()-1){
                    arrys.remove(position);
                    notifyItemRemoved(position);
                }else{
                    SparseArray<T> tempArray=arrys;
                    for(int i=position;i<tempArray.size()-1;i++){
                        arrys.put(i,tempArray.get(i+1));
                    }
                    arrys.remove(getItemCount()-1);
                    notifyDataSetChanged();

                }
            }
        });
    }

    public void addAll(final SparseArray<T> items) {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int startIndex = RecyclerViewBaseAdapter.this.arrys.size();
                for(int i=0;i<items.size();i++){
                    int key=items.keyAt(i);
                    RecyclerViewBaseAdapter.this.arrys.put(startIndex + i, items.get(key));
                }
                notifyItemRangeInserted(startIndex, items.size());
            }
        });
    }

    public void clear() {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int size = arrys.size();
                arrys.clear();
                notifyItemRangeRemoved(0, size);
            }
        });
    }
}